package com.kaoshi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaoshi.agency.domain.Qianyue;
import com.kaoshi.agency.mapper.QianyueMapper;
import com.kaoshi.entity.*;
//import com.kaoshi.entity.Qianyue;
import com.kaoshi.management.pojo.XtManagement;
import com.kaoshi.mapper.GlMapper;
import com.kaoshi.mapper.JkjlMapper;
import com.kaoshi.mapper.PersonMapper;
//import com.kaoshi.mapper.QianyueMapper;

import com.kaoshi.service.Feignagency;
//import com.kaoshi.service.Feignziliaoguanli;
//import com.kaoshi.service.Feignmanagement;
import com.kaoshi.service.Feignmanagement;
import com.kaoshi.service.Feignziliaoguanli;
import com.kaoshi.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-25
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {

    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private GlMapper glMapper;

    @Autowired
    private JkjlMapper jkjlMapper;

    @Autowired
    private Feignagency feignagency;
    @Autowired
    private QianyueMapper qianyueMapper;

    @Autowired
    private Feignziliaoguanli feignziliaoguanli;

    @Autowired
    private Feignmanagement feignmanagement;

    @Override
    public Page<Person> findList(Integer current, Integer pageSize, Person person) {
        Page<Person> page = new Page<>(current, pageSize);
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        if (person.getQystatus() != null) {
            personQueryWrapper.eq("qystatus", person.getQystatus());
        }
        if (person.getQyjg() != null && !person.getQyjg().equals("")) {
            personQueryWrapper.eq("qyjg", person.getQyjg());
        }

        if (person.getYstd() != null && !person.getYstd().equals("")) {
            personQueryWrapper.eq("ystd", person.getYstd());
        }
        if (person.getFwb() != null && !person.getFwb().equals("")) {
            personQueryWrapper.eq("fwb", person.getFwb());
        }
        if(person.getName()!=null && !person.getName().equals("")){
            personQueryWrapper.like("name", person.getName());
        }
        if (person.getBq() != null && !person.getBq().equals("")) {
           if(person.getBq().contains(",")){
               String[] split = person.getBq().split(",");
               personQueryWrapper.nested(wrapper -> {
                   for (int i = 0; i < split.length; i++) {
                       String s = split[i];
                       if (i > 0) {
                           wrapper.or();
                       }
                       wrapper.eq("bq", s);
                   }
               });
           }else{
               personQueryWrapper.eq("bq", person.getBq());
           }

        }
        return baseMapper.selectPage(page, personQueryWrapper);
    }

    @Override
    public List<Person> hqgl(Integer id) {
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        if (id != null) {
            personQueryWrapper.notIn("id", id);
        }
        return baseMapper.selectList(personQueryWrapper);
    }


    @Override
    public List<Person> mhList(Gl gl) {
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        personQueryWrapper.notIn("id", gl.getPid());
        if (gl.getName() != null && !gl.getName().equals("")) {

            personQueryWrapper.like("name", gl.getName());

        }
        return baseMapper.selectList(personQueryWrapper);
    }

    @Override
    public List<Person> yhgl(Gl gl) {
       List<Person> personList= personMapper.yhgl(gl);
       return personList;
    }


    @Override
    public void addJm(Person person) {
        //1.新增person
        person.setJmstatus(1);
        person.setQystatus(2);
        personMapper.insert(person);
        //2.新增关联表
        Gl gl = new Gl();
        gl.setPid(person.getId());
        gl.setGid(person.getGid());
        gl.setGx(person.getGx());
        gl.setDel(0);
        glMapper.insert(gl);
        Gl gl1 = new Gl();
        gl1.setPid(person.getGid());
        gl1.setGid(person.getId());
        gl1.setGx(person.getGx());
        gl1.setDel(0);
        glMapper.insert(gl1);
    }


    @Override
    public Person getBySfz(Person person) {
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        personQueryWrapper.eq("sfz",person.getSfz());
        return baseMapper.selectOne(personQueryWrapper);
    }


    @Override
    public List<Person> getByPid(Gl gl) {
        List<Person> yhgl = yhgl(gl);

        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        for (Person person : yhgl) {
            personQueryWrapper.notIn("id",person.getId());
        }
        personQueryWrapper.notIn("id",gl.getPid());
        if (gl.getName() != null && !gl.getName().equals("")) {
            personQueryWrapper.like("name", gl.getName());
        }
        List<Person> personList = baseMapper.selectList(personQueryWrapper);
        System.out.println(Arrays.toString(personList.toArray()));
        return personList;
    }


    @Override
    public Integer updatee(Person person) {

        if(person.getId()!=null){
            personMapper.updateById(person);
        }else{
            person.setJmstatus(1);
            person.setQystatus(2);
            personMapper.insert(person);
        }
//        if(person.getGid()!=null){
            Gl gl = new Gl();
            gl.setPid(person.getId());
            gl.setGid(person.getGid());
            gl.setGx(person.getGx());
            glMapper.insert(gl);
            System.out.println(gl);
            Gl gl1 = new Gl();
            gl1.setPid(person.getGid());
            gl1.setGid(person.getId());
            gl1.setGx(person.getGx());
            System.out.println("___________________"+gl);
            System.out.println("_____________________________"+gl1);
            glMapper.insert(gl1);
            System.out.println(gl1);
//        }
        Jkjl jkjl = new Jkjl();
        jkjl.setLj(0);
        jkjl.setSg(person.getSg());
        jkjl.setZl(person.getZl());
        jkjl.setXx(person.getXx());
        jkjl.setYx(person.getYx());
        jkjl.setGms(person.getGms());
        jkjl.setJwd(person.getJwd());
        jkjl.setJzs(person.getJzs());
        jkjl.setJzbs(person.getJzbs());
        if(person.getJid()!=null){

            jkjlMapper.updateById(jkjl);
        }else{
            jkjl.setPid(person.getId());
            jkjlMapper.insert(jkjl);
        }

        return 1;

    }

    @Override
    public void delete(Integer id) {
        personMapper.deleteById(id);
        QueryWrapper<Jkjl> jkjlQueryWrapper = new QueryWrapper<>();
        jkjlQueryWrapper.eq("pid",id);
        jkjlMapper.delete(jkjlQueryWrapper);
        QueryWrapper<Gl> glQueryWrapper = new QueryWrapper<>();
        glQueryWrapper.eq("pid",id);
        glMapper.delete(glQueryWrapper);
    }


    @Override
    public List<Qianyue> qy(Integer id) {
        List<Qianyue> byBh = feignagency.getByBh(id);
        List<Institution> byId = feignziliaoguanli.findAll();
        List<Team> teams = feignziliaoguanli.TfindAll();
        List<Doctor> doctor = feignziliaoguanli.getDoctor();
        List<Bao> bao = feignziliaoguanli.getBao();
        for (Qianyue qianyue : byBh) {
            for (Institution institution : byId) {
               if(qianyue.getJid()==institution.getId()){
                   qianyue.setJname(institution.getOrganization());
               }
            }
            for (Team team : teams){
                if(qianyue.getTid()==team.getId()){
                    qianyue.setTname(team.getTeamname());
                }
            }
            for (Doctor doctor1 : doctor){
                if(qianyue.getYid()==doctor1.getId()){
                    qianyue.setYname(doctor1.getName());
                }
            }
            for (Bao bao1 : bao){
                if(qianyue.getFid()==bao1.getId()){
                    qianyue.setFname(bao1.getName());
                }
            }
        }
        return byBh;
    }

    @Override
    public List<Team> team() {
        return feignziliaoguanli.TfindAll();
    }

    @Override
    public List<Bao> bao() {
        List<Bao> bao = feignziliaoguanli.getBao();
        return bao;
    }

    @Override
    public List<Institution> jg() {
        List<Institution> all = feignziliaoguanli.findAll();
        return all;
    }

    @Override
    public List<Person> findAll() {
        return personMapper.findAll();
    }

    @Override
    public List<Person> findAll1() {
        return personMapper.findAll1();
    }


    @Override
    public List<XtManagement> getBq() {
        return feignmanagement.getBq();
    }
}
